package Controleurs;

import partage.Coordonne;
import partage.Coup;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Message;
import android.projet.morpion.AndroidActivity;
import android.projet.morpion.R;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;
import client.Client;
import client.IdMessage;

public class ControleurClient extends Controleur implements
		android.view.View.OnClickListener {

	private int versionJeu = 0;
	private Client client;
	private ImageView[][] imageViews;
	private Bitmap[] bitmap;

	public ControleurClient(AndroidActivity activity) {
		super(activity, R.layout.client, null);
	}

	/**
	 * @return the versionJeu
	 */
	public int getVersionJeu() {
		return versionJeu;
	}

	public void setVersionJeu(int versionJeu) {
		this.versionJeu = versionJeu;
	}

	/**
	 * @param coup
	 */
	public void updateVue(Coup coup) {
		try {
			System.out.println("updateVue : " + coup);
			imageViews[coup.getCoord().getX()][coup.getCoord().getY()]
					.setImageBitmap(bitmap[coup.getSymb().ordinal() - 1]);
			setMain(coup.getSymb().ordinal() - 1);
		} catch (IllegalAccessError exp) {
			System.out.println(exp);
		} catch (IllegalArgumentException exparg) {
		}
	}

	@Override
	protected void initVue() {
		super.initVue();
		chargerImage();
		client = new Client("Morpion", this);
		client.start();
	}

	private void chargerImage() {
		bitmap = new Bitmap[2];
		if (versionJeu==1) {
			bitmap[0] = BitmapFactory.decodeResource(activity.getResources(),
					R.drawable.acarien);
			bitmap[1] = BitmapFactory.decodeResource(activity.getResources(),
					R.drawable.tique);

		} else {
			bitmap[0] = BitmapFactory.decodeResource(activity.getResources(),
					R.drawable.beepbeep);
			bitmap[1] = BitmapFactory.decodeResource(activity.getResources(),
					R.drawable.bugsbunny);
		}
	}

	public void consVue(int taille) {

		try {
			TableLayout linearLayout = (TableLayout) activity
					.findViewById(R.id.tableLayout1);
			LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
			WindowManager wm=(WindowManager)activity.getSystemService(Context.WINDOW_SERVICE);
			   Display display= wm.getDefaultDisplay();
			   int minheight = (display.getHeight()-10)/(taille+1);
			   int minwight = (display.getWidth())/(taille+1);
			  // int maxHeight = display.getHeight()/(taille+1);
			 //  int maxWight = display.getWidth()/(taille+1);
			
			imageViews = new ImageView[taille][taille];
			linearLayout.setBackgroundColor(Color.LTGRAY);
			linearLayout.setLayoutParams(params);
			for (int i = 0; i < taille; i++) {
				LinearLayout layout = new LinearLayout(activity);
				layout.setOrientation(LinearLayout.HORIZONTAL);
				imageViews[i] = new ImageView[taille];
				for (int j = 0; j < taille; j++) {
					imageViews[i][j] = new ImageView(activity);
					imageViews[i][j].setMinimumWidth(minwight);
					imageViews[i][j].setMinimumHeight(minheight);
					//imageViews[i][j].setMaxHeight(maxHeight);
					//imageViews[i][j].setMaxWidth(maxWight);
					imageViews[i][j].setImageResource(R.drawable.morpion);
					imageViews[i][j].setTag(new Coordonne(i, j));
					imageViews[i][j].setOnClickListener(this);
					imageViews[i][j].setPadding(6, 0, 5, -5);
					//imageViews[i][j].setPadding(5, 0, 5, 0);
					layout.addView(imageViews[i][j]);
				}
				linearLayout.addView(layout);
			}
			LinearLayout layout = (LinearLayout) activity
					.findViewById(R.id.linearLayout1);
			layout.setBackgroundColor(Color.WHITE);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	@Override
	public void touchRetourAction() {
		client.interrupt();
		activity.setControleur(precedant);
		precedant.demarrer();
	}

	@Override
	public void recMessage(Message msg) {
		if (msg.what == IdMessage.DataJeuCoup.ordinal()) {
			updateVue((Coup) msg.obj);
			return;
		}

		if (msg.what == IdMessage.AlertFinJeu.ordinal()) {
			finJeu((Boolean) msg.obj);
			return;
		}
		if (msg.what == IdMessage.DonneesReseaux.ordinal()) {
			client.traitement((String) msg.obj);
			return;
		}
		if (msg.what == IdMessage.CoordJouer.ordinal()) {
			client.updateCoup((Coordonne) msg.obj);
			return;
		}
		super.recMessage(msg);
	}

	@Override
	public void touchMenuAcion(Menu menu) {

	}

	public void setSymb(int icone, int i) {
		try {
			ImageView view;
			if (i == 0)
				view = (ImageView) activity.findViewById(R.id.imgJoueur1);
			else
				view = (ImageView) activity.findViewById(R.id.imgJoueur2);
			view.setImageBitmap(bitmap[icone]);
			view.postInvalidate();
		} catch (Exception e) {
			System.out.println(e.getLocalizedMessage());
		}
	}

	public void setNomJoueur(String nomJoueur, int i) {
		TextView nomjour;
		if (i == 0)
			nomjour = (TextView) activity.findViewById(R.id.pseudoJoueur1);
		else
			nomjour = (TextView) activity.findViewById(R.id.pseudoJoueur2);
		nomjour.setText(nomJoueur);
		nomjour.setTextColor(Color.BLACK);
	}

	public void setMain(int i) {
		try {
			ImageView view = (ImageView) activity.findViewById(R.id.imgMain);
			view.setImageBitmap(bitmap[i]);
			view.postInvalidate();
		} catch (Exception e) {
			System.out.println(e.getLocalizedMessage());
		}

	}

	public void finJeu(boolean partie) {
		if (!partie)
			touchRetourAction();
	}

	@Override
	public void onClick(View v) {
		if (v.getTag() instanceof Coordonne) {
			Coordonne coordonne = (Coordonne) v.getTag();
			if (client.getModele().estVide(coordonne))
				client.updateCoup(coordonne);

		}
	}
}
